﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.ProviderBase; 

public partial class MasterPage : System.Web.UI.MasterPage
{
    
    public int treeID;
    public int position = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Timeout=1440;
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
        Label1.Text = Session["name"].ToString();
        Menu1.Items.Clear();
        SqlConnection CN = new SqlConnection("server=localhost;uid=sa;pwd=1234;database=WMS"); //MENUDB是数据库名 
      //  SqlConnection CN = new SqlConnection("server=ftp1;uid=sa;pwd=1234;database=Leave");
        //设定数据库操作命令 
        CN.Open();
        SqlCommand Scmd = new SqlCommand("select * from menu where treeId=0 and quanxian like '%" + Session["power"].ToString() + "%' order by position", CN); //选出根菜单 
        SqlDataReader odr = Scmd.ExecuteReader();
        while (odr.Read()) //循环读取 
        {
            MenuItem menuNode = new MenuItem();
            menuNode.Text = odr["names"].ToString();
            menuNode.Value = odr["id"].ToString();
            menuNode.Enabled = true;
            menuNode.NavigateUrl = odr["page"].ToString();
            Menu1.Items.Add(menuNode); //添加到根菜单 
            treeID = Convert.ToInt16(odr["position"].ToString());
           // position=Convert.ToInt16(odr["position"].ToString());
            addchildmenu(menuNode);
          
        }
        Scmd.Connection.Close(); 
    }
    protected void addchildmenu(MenuItem pnode) //添加子菜单 
{
   // SqlConnection CN = new SqlConnection("server=ftp1;uid=sa;pwd=1234;database=Leave");
    SqlConnection CN = new SqlConnection("server=localhost;uid=sa;pwd=1234;database=WMS"); 
     //设定数据库操作命令 
     CN.Open();
     SqlCommand Scmd1 = new SqlCommand("select * from menu where treeID=" + treeID + " and quanxian like '%" + Session["power"].ToString() + "%'", CN); 

     SqlDataReader odr = Scmd1.ExecuteReader(); 
     while (odr.Read()) 
    { 
        MenuItem menuNode = new MenuItem(); 
        menuNode.Text = odr["names"].ToString(); 
        menuNode.Value = odr["id"].ToString(); 
        menuNode.Enabled = true; 
        menuNode.NavigateUrl = odr["page"].ToString(); 
        pnode.ChildItems.Add(menuNode); //为当前菜单项添加子菜单 
      //  treeID = Convert.ToInt16(odr["id"].ToString()); 
     //   treeID = position;
       // addchildmenu(menuNode); //循环添加子菜单 
     } 
        //关闭数据库连接 
          Scmd1.Connection.Close(); 
    } 
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Login.aspx?");
    }
}
